<script lang="ts" setup>

import {func, globalConfig} from "../js/Data.ts";
import {EnergyMachine} from "../js/EnergyMachine";
import allItemInfo from "../js/ItemInfo";

let pem = globalConfig.current.planet.pem

let addEnergyMachine = (e: EnergyMachine) => {
  for (let em of pem) {
    if (em.name != e.name) {
      continue
    }
    // 是否达到上限
    if (em.now >= em.max) {
      return
    }

    // 是否有足够面积
    if (allItemInfo().get(e.name).ia > globalConfig.current.planet.pa * globalConfig.current.planet.plp - globalConfig.current.planet.pau) {
      return
    }

    // 是否有足够物品
    if (globalConfig.inventory.get(e.name).ic < 1) {
      return
    }

    globalConfig.inventory.get(e.name).ic -= 1
    em.now += 1

    globalConfig.current.planet.pe += em.power
  }
}

</script>

<template>
  <!--  <n-space>-->
  <!--    <n-button-group>-->
  <!--      <n-button :type="'' == globalConfig.filter.inventoryType ? 'primary' : 'default'" style="width: 100px"-->
  <!--                @click="globalConfig.filter.inventoryType = ''">-->
  <!--        全部-->
  <!--      </n-button>-->
  <!--      <n-button :type="'has' == globalConfig.filter.inventoryType ? 'primary' : 'default'" style="width: 100px"-->
  <!--                @click="globalConfig.filter.inventoryType = 'has'">-->
  <!--        拥有-->
  <!--      </n-button>-->
  <!--      <n-button :type="'full' == globalConfig.filter.inventoryType ? 'primary' : 'default'" style="width: 100px"-->
  <!--                @click="globalConfig.filter.inventoryType = 'full'">-->
  <!--        已满-->
  <!--      </n-button>-->
  <!--    </n-button-group>-->
  <!--  </n-space>-->
  <n-table :bordered="false" style="margin-top: 10px">
    <thead>
    <tr>
      <th>电机</th>
      <th>功率</th>
      <th>总功率</th>
      <th>已分配数量</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr v-for="e in pem">
      <td>
        <n-image-group>
          <n-space>
            <n-image
                :src=func.srcHandler(e.name)
                width="32"
            />
          </n-space>
        </n-image-group>
      </td>
      <td>
        {{ e.power }}
      </td>
      <td>
        {{ e.power * e.now }}
      </td>
      <td>
        {{ e.now }} / {{ e.max }}
      </td>
      <td>
        <n-button-group>
          <n-button
              type="default"
              @click="addEnergyMachine(e)"
          >
            增加电机
          </n-button>
        </n-button-group>
      </td>
    </tr>
    </tbody>
  </n-table>
</template>

<style scoped>

</style>